//
//  ManagerSchoolFMDB.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/10/12.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "ManagerSchoolFMDB.h"
#import "SpaceModel.h"
#import "AreaModel.h"

@interface ManagerSchoolFMDB ()

// 数据库
@property (nonatomic, strong) FMDatabase *db;

@end


@implementation ManagerSchoolFMDB

// 创建数据库
- (void)createFMDB{
    NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"ARTS_FMDB.db"];
    _db = [FMDatabase databaseWithPath:dbPath];
    
    if (![_db open]) {
        return;
    }
    
    if(![_db tableExists:@"Arealist"]){
        [_db executeUpdate:@"CREATE TABLE Arealist (area_Id text primary key,place text, backImg text)"];
    }
}

// 向数据库中添加数据
- (void)addNewsIntoDataBase:(AreaModel *)model{
    [_db executeUpdate:@"INSERT INTO Arealist (area_Id, place, backImg) VALUES (?,?,?)",model.area_Id,model.place,model.backImg];
}

// 从数据库中获取数据
- (NSMutableArray *)searchNewsmodelFromDB{
    NSMutableArray *marr = [NSMutableArray array];
    NSString *query = @"SELECT * FROM Arealist";
    FMResultSet * rs = [_db executeQuery:query];
    
    while ([rs next]) {
        AreaModel *model = [[AreaModel alloc] init];
        model.area_Id = [rs stringForColumn:@"area_Id"];
        model.place = [rs stringForColumn:@"place"];
        model.backImg = [rs stringForColumn:@"backImg"];
        [marr addObject:model];
    }
    [rs close];
    return marr;
}

// 从数据库中删除某个特定的数据
- (void)deleteDataFromDB:(AreaModel *)model{
    [_db executeUpdate:@"delete from Foodlist where name = ?", model.place];
}





@end
